﻿#include <Windows.h>
#include <stdio.h>
#include "messageQueue.h"


HANDLE CreateMessageQ(KEY key)
{
	return CreateMailslot(key,
 		0,  
 		MAILSLOT_WAIT_FOREVER,
 		(LPSECURITY_ATTRIBUTES) NULL);
}

HANDLE OpenMessageQ (KEY key)
{
	return  CreateFile(key,
 	GENERIC_WRITE, 
 	FILE_SHARE_READ | FILE_SHARE_WRITE,  
 	(LPSECURITY_ATTRIBUTES) NULL,
 	OPEN_EXISTING, 
 	FILE_ATTRIBUTE_NORMAL,
 	(HANDLE) NULL);    
}

void CloseMessageQueue (HANDLE hMsgQueue)
{
	CloseHandle(hMsgQueue);
}

BOOL SendMessageQueue (HANDLE hMsgQueue, tMsg *src)
{
	unsigned long cbWritten;
	
	return WriteFile(hMsgQueue,
 	src,
 	sizeof(*src),
 	&cbWritten,
 	(LPOVERLAPPED) NULL);
}

BOOL ReceiveMessageQueue (HANDLE hMsgQueue, tMsg *dest)
{
	unsigned long cbRead;
 
	return ReadFile(hMsgQueue,
 		dest,
 		sizeof(*dest),
 		&cbRead,
 		(LPOVERLAPPED) NULL);
}

/*BOOL MessageInQueue(HANDLE hSlot)
{
	DWORD cbMessage,cMessage;
	BOOL fResult = GetMailslotInfo( hSlot, // mailslot handle 
        (LPDWORD) NULL,               // no maximum message size 
        &cbMessage,                   // size of next message 
        &cMessage,                    // number of messages 
        (LPDWORD) NULL);              // no read time-out 
 
    if (!fResult) 
    { 
        printf("GetMailslotInfo failed with %d.\n", GetLastError()); 
        return FALSE; 
    } 
 
    if (cbMessage == MAILSLOT_NO_MESSAGE) 
    {  
        return FALSE; 
    }
	return TRUE;
}
 */
   